//
//  CardViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/30.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import hpple
import SwiftyJSON

class CardViewController: KMViewController {
    
    fileprivate let cardView = CardScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = handleCard
        addSubView()
        
    }
    
    
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        cardView.cardDelegate = self
        self.view.addSubview(cardView)
        
        cardView.bankView.moreButton.addTarget(self, action: #selector(moreBankType), for: .touchUpInside)
        cardView.bankView.arrowButton.addTarget(self, action: #selector(moreBankType), for: .touchUpInside)
        cardView.recommendView.moreButton.addTarget(self, action: #selector(moreRecommend), for: .touchUpInside)
        cardView.recommendView.arrowButton.addTarget(self, action: #selector(moreRecommend), for: .touchUpInside)
        cardView.strategyView.moreButton.addTarget(self, action: #selector(moreStrategy), for: .touchUpInside)
        cardView.strategyView.arrowButton.addTarget(self, action: #selector(moreStrategy), for: .touchUpInside)
        
        cardView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        
    }
    
    deinit {
        
        cardView.noticeView.timer.invalidate()
        cardView.noticeView.timer = nil
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    //MARK: 更多银行
    @objc fileprivate func moreBankType() {
        
        let handleVC = HandleViewController.init()
        handleVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(handleVC, animated: true)
    }
    
    //MARK: 更多推荐
    @objc fileprivate func moreRecommend() {
        
        let handleVC = HandleViewController.init()
        handleVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(handleVC, animated: true)
    }
    
    //MARK: 更多攻略
    @objc fileprivate func moreStrategy() {
        
        inquireStrategy(index: 0)
        
    }
    
    //MARK: 攻略查询
    fileprivate func inquireStrategy(index:Int,isRaised:Bool?=false,isWool:Bool?=false) {
        
        let rootVC = UIApplication.shared.keyWindow?.rootViewController as? RootTabBarController
        rootVC?.selectedIndex = 2
        let nav = rootVC?.viewControllers![2] as? KMNavigationController
        let informationenVC = nav?.rt_visibleViewController as? InformationRootViewController
        informationenVC?.currentVC = index
        
        //提额
        if isRaised! {
            
            let second:Double = 0.01
            let dtime = DispatchTime.now() + Double(Int64(second * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
            DispatchQueue.main.asyncAfter(deadline: dtime) {() ->
                Void in
                
               NotificationCenter.default.post(name: .amountRaisedPush, object: nil, userInfo: ["index":"10"])
            }
            
        }
        
        //热门羊毛
        if isWool! {
            
            let second:Double = 0.1
            let dtime = DispatchTime.now() + Double(Int64(second * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
            DispatchQueue.main.asyncAfter(deadline: dtime) {() ->
                Void in
                
                NotificationCenter.default.post(name: .popularWoolPush, object: nil, userInfo: ["index":"6"])
            }
            
        }
        
    }

}

extension CardViewController : CardScrollViewDelegate {
    
    //MARK: 选择轮播图
    func selectCardBanner(url: String) {
        
        let webVC = SpecialWebViewController.init(urlString:url, title: self.title)
        webVC.hidesBottomBarWhenPushed = true
        webVC.removeElements = ["header","app-download","footer"]
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
    //MARK: 显示公告
    func showNotice() {
        
        let noticeWebVC =  NoticeWebViewController.init(urlString: webUrl + "/mobile/noticedetail/1", title: netNotice)
        noticeWebVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(noticeWebVC, animated: true)
    }
    
    //MARK: 免费申请详情
    func showRecommendDetails(id: String,urlStr: String,title:String) {
        
        KMTool.generateRecords(idString: id)
      
        let webDetailsVC = WebDetailsViewController.init(urlString: urlStr)
        webDetailsVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webDetailsVC, animated: true)
    }
    
    //MARK:  显示新闻详情
    func showNewsDetails(model:RecommendModel) {
        
        let urlString = webUrl + "/mobile/details/i" + model.id!
        
        
        let webVC = WebViewController.init(urlString: urlString, title: model.credit_name, shareImage:kmHttps + model.image!, shareText: model.favorable)
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
    //MARK: 显示攻略详情
    func showStrategyDetails(model: NewsModel) {
        
        let webVC = WebViewController.init(urlString: kmHttps + model.href!, title: model.title, shareImage: kmHttps + model.image!, shareText: model.text,shareTitle:model.title)
        webVC.removeElements = ["mblocks","crumbs mblock","app-download","mfooter mblock footer1","bshare-custom icon-medium"]
        webVC.removeIds = ["xiong_div"]
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
    //MARK: 选择攻略标签
    func selectStrategyLabel(index: Int) {
        
        switch index {
        case 0: //信用卡资讯
            self.inquireStrategy(index: 0)
        case 1: //信用卡服务
            self.inquireStrategy(index: 1)
        case 2: //卡神学堂
            self.inquireStrategy(index: 2)
        case 3: //提额
            self.inquireStrategy(index: 1,isRaised: true)
        case 4: //热门羊毛
            self.inquireStrategy(index: 0,isWool: true)
        case 5: //卡优惠
            let webVC = CardOfferViewController.init(urlStr: webUrl + "/myouhuis", title: cardOffer)
            webVC.hidesBottomBarWhenPushed = true
            webVC.removeElements = ["mblock mblocks","app-download","footer"]
            self.navigationController?.pushViewController(webVC, animated: true)
        default:
            break
        }
    }
    
    //MARK: 选择服务类型
    func selectType(title: String) {

        switch title {
        case cardArtifact://办卡神器
            let cardToolVC = CardToolViewController.init(title: title)
            cardToolVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(cardToolVC, animated: true)
        case liftingCheats://提额秘籍
            let improveVC = ImproveViewController.init(urlString: "/mobile/forehead", title: title)
            improveVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(improveVC, animated: true)
        case progressInquiry://进度查询
            let progressVC = ProgressViewController.init(title: progressQuery)
            progressVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(progressVC, animated: true)
        case creditCardActivation://信用卡激活
            let progressVC = ProgressViewController.init(title: creditCardActivation)
            progressVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(progressVC, animated: true)
        case creditCardTopic://信用卡专题
            let specialVC = SpecialViewController.init(title: title, urlString: "/mobile/zhuanti")
            specialVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(specialVC, animated: true)
        case appointmentCard://预约办卡
            break
        default:
            break
        }
        
        
    }
    
    //MARK: 选择银行类型
    func selectBankType(title: String,image: String) {
        
        if title == GuangfaBank || title == EverbrightBank {
            let handleVC = HandleViewController.init(bankType: title)
            handleVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(handleVC, animated: true)
        }else{
            let handleVC = CardCenterViewController.init(title: title, imageUrl: image)
            handleVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(handleVC, animated: true)
        }
        
        
    }
    
    //MARK: 选择卡片类型
    func selectCardType(title: String) {
        
        let handleVC = HandleViewController.init(useType: title)
        handleVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(handleVC, animated: true)
    }
}
